package com.share.controller;

import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import com.share.Rotem.StationService;
import com.share.common.core.web.domain.AjaxResult;
import com.share.common.security.annotation.RequiresLogin;
import com.share.domain.Station;
import com.share.service.DeviceService;
import java.math.BigDecimal;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

import static com.share.common.core.web.domain.AjaxResult.success;

@RestController
@RequestMapping("/station")
public class DeviceApiController {
     @Autowired
     private DeviceService deviceService;
     @Autowired
     private StationService stationService;

     @Operation(summary = "根据经纬度搜索附近门店（站点）")
     @RequiresLogin
     @GetMapping("/nearbyStation/{latitude}/{longitude}")
     public AjaxResult nearbyStation(@PathVariable String latitude, @PathVariable String longitude)
     {
        // 根据经纬度获取附近的站点列表，最多返回10个站点
        List<Station> stationList = deviceService.nearbyStation(latitude, longitude, 10);
        // 如果没有找到附近的站点，则更新站点数据
        if (CollectionUtils.isEmpty(stationList)) {
            Station station = new Station();
            station.setLatitude(new BigDecimal(latitude));
            station.setLongitude(new BigDecimal(longitude));
            stationService.setData(station);
        }
         return success(stationList);
     }

    @PostMapping("/updateData")
    public AjaxResult updateData(@RequestBody Station station) {
        // 更新指定站点数据
        return stationService.setData(station);
    }
}